package arocms.upload;

import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.upload.FormFile;

public class UploadFileForm extends org.apache.struts.action.ActionForm {
    
    private String title;
    private String description;
    private FormFile file;
    private static final int MAX_FILE_SIZE = 10485760; //10MB
    
    public UploadFileForm() {
        super();
        // TODO Auto-generated constructor stub
    }
    
    public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {
        ActionErrors errors = new ActionErrors();
        if (getTitle()==null || getTitle().length()<1) errors.add("title",new ActionMessage("errors.required","Title"));
        if (getDescription()==null || getDescription().length()<1) errors.add("description",new ActionMessage("errors.required","Description"));
        if (getFile()==null || getFile().getFileSize()<1) errors.add("file", new ActionMessage("errors.required","File"));
        if (getFile()!=null && getFile().getFileSize()>MAX_FILE_SIZE) errors.add("file", new ActionMessage("errors.maxfilesize", String.valueOf(getFile().getFileSize()), String.valueOf(MAX_FILE_SIZE)));
        return errors;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public FormFile getFile() {
        return file;
    }

    public void setFile(FormFile file) {
        this.file = file;
    }
}
